/**
 *  Copyright (C) 2011  Kyle Thayer <kyle.thayer AT gmail.com>
 *
 *  This file is part of the IFCSoft project (http://ifcsoft.com)
 *
 *  IFCSoft is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ifcSoft.view;

import ifcSoft.MainApp;
import ifcSoft.view.MainMediator;
import ifcSoft.model.DataSetProxy;
import ifcSoft.view.dialogBox.ifcDialogBox;
import ifcSoft.view.dialogBox.ifcDialogDataSetSelect;
import java.io.BufferedWriter;
import java.io.FileWriter;
import javax.swing.JFileChooser;
import ifcSoft.view.fileFilter.CSVFileFilter;
import ifcSoft.view.fileFilter.FCSFileFilter;
import ifcSoft.view.fileFilter.DataFileFilter;
import java.io.File;
import ifcSoft.model.dataSet.summaryData.SummaryData;
import java.io.IOException;

/**
 *  @author Kyle Thayer <kthayer@emory.edu>
 */

public class SaveDataSetDialog {
  public-init var app:MainApp;
  public-init var mainMediator:MainMediator;
  postinit{
    if(app == null or mainMediator == null){
      println("SaveDataSetDialog: not initialized fully");
    }else{
      initialize();
    }
  }

  var saveSetDialog:ifcDialogBox;
  var dataSetSelect:ifcDialogDataSetSelect;


  public function initialize(){
    dataSetSelect = ifcDialogDataSetSelect{mainApp:app};

    saveSetDialog =  ifcDialogBox{
      name: "Save Data Set"
      okAction: saveDataSetOK
      content: [dataSetSelect]
      cancelAction: function():Void{app.removeDialog(saveSetDialog)}

      blocksMouse: true
    };

    app.addDialog(saveSetDialog);
  }

  function saveDataSetOK():Void{
    var datasets = (dataSetSelect.getDataSets());
    if(datasets.size() == 0){
      app.alert("No data set selected");
      app.unblockContent();
      return;
    }

    var finaldsp:DataSetProxy = mainMediator.getDataSet(dataSetSelect.getDataSets());
    if(finaldsp == null){
      println("Error in data set combination");
      return;
    }

    saveDataSet(finaldsp);

    app.removeDialog(saveSetDialog);
  }

  function saveDataSet(dsp:DataSetProxy):Void{
    var fileChooser:JFileChooser;
    if(app.lastFilePath == null){
      fileChooser = new JFileChooser();
    }else{
      fileChooser = new JFileChooser(app.lastFilePath);
    }
    var filter:CSVFileFilter = new CSVFileFilter();
    fileChooser.setFileFilter(filter);

    if(fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION){

      app.lastFilePath = fileChooser.getCurrentDirectory().getAbsolutePath();
      var files: File = fileChooser.getSelectedFile();
      println("saving: {files.getAbsolutePath()}");
      var newFilename =files.getAbsolutePath();

      var bw:BufferedWriter;
      try {
        bw = new BufferedWriter(new FileWriter(newFilename));
        if(dsp.getData() instanceof SummaryData){
          bw.write("name,");
        }

        for(dim in dsp.getColNames()){
          bw.write(dim);
          if(indexof dim == dsp.getColNames().length-1){
            bw.newLine();
          }else{
            bw.write(",");
          }
        }


        for(i in [0..dsp.getDataSize()-1]){
          if(dsp.getData() instanceof SummaryData){
            bw.write("{dsp.getData().getPointName(i)},");
          }

          for(j in [0..dsp.getColNames().length-1]){
            bw.write("{dsp.getData().getVals(i)[j]}");
            if(indexof j == dsp.getColNames().length-1){
              bw.newLine();
            }else{
              bw.write(",");
            }
          }

        }
        bw.close();

      }catch(ex:IOException){
        //if(ex.)
        println("IO Error:{ex}");
        return;
      }

    }


  }


}
